﻿Shader "SongShaderDemo/reflectDemo"
{
    Properties
    {
		_CubeTex("Cube Tex", Cube) = "white"{}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
               // float2 uv : TEXCOORD0;
				float3 normal: NORMAL;
            };

            struct v2f
            {
                //float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
				float3 rDir : TEXCOORD1;
            };
			uniform samplerCUBE _CubeTex;
			// 定义了反射函数,内部是前面推导后的计算方式
			float3 reflection(float3 i, float3 n) {
				return i - 2 * n * dot(i, n);
			}

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
				float3 nor = mul(v.normal, (float3x3)unity_WorldToObject);
				// 从顶点到相机的标准化向量
				float3 viewDir = normalize(WorldSpaceViewDir(v.vertex));
				// 作为入射向量进行计算
				o.rDir = reflection(-viewDir, normalize(nor));
				return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
              
				fixed4 col = texCUBE(_CubeTex, i.rDir);
                return col;
            }
            ENDCG
        }
    }
}
